package com.weight.common.generate.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

/**
*  创建非java文件
*  
*/
public class ResourceFile {
	protected String fileDir;// 文件路径
	protected String fileName;//文件名
	protected String resource;//文件內容
	
	public ResourceFile(String fileDir, String fileName) {
		super();
		this.fileDir = fileDir;
		this.fileName = fileName;
	}
	
	protected void init(String resource) {
		this.resource = resource;
	}
	/**
	 * 创建文件路径文件夹
	 * @return
	 * @throws Exception
	 */
	protected File makeResourceDir() throws Exception{
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir;
    }
	/**
	 * 创建文件
	 * @return
	 * @throws Exception
	 */
    protected File makeResourceFile() throws Exception{
    	File dir = makeResourceDir();
        File file = new File(dir, fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }
    //文本覆盖
    public void generate() throws Exception{
       
        File file = makeResourceFile();
        FileWriter fileWriter = new FileWriter(file);
       
        StringBuilder stringBuilder = new StringBuilder();
       
        if(resource != null) {
        	stringBuilder.append(resource);
        }
       
        fileWriter.write(stringBuilder.toString());
         
        fileWriter.flush();
        fileWriter.close();
    }
    //文本追加
    public void geneAppend() throws Exception{
        StringBuilder stringBuilder = new StringBuilder("\r\n");
        if(resource != null) {
        	stringBuilder.append(resource);
        	stringBuilder.append("\r\n");
        	File file = makeResourceFile();
		    RandomAccessFile randomFile = null;  
		    try {     
		        // 打开一个随机访问文件流，按读写方式     
		        randomFile = new RandomAccessFile(file, "rw");     
		        // 文件长度，字节数     
		        long fileLength = randomFile.length();     
		        // 将写文件指针移到文件尾。     
		        randomFile.seek(fileLength);     
		        randomFile.write(stringBuilder.toString().getBytes());       
		    } catch (IOException e) {     
		        e.printStackTrace();     
		    } finally{  
		        if(randomFile != null){  
		            try {  
		                randomFile.close();  
		            } catch (IOException e) {  
		                e.printStackTrace();  
		            }  
		        }  
		    }
        }
    }
    //文本插入一条语句
    public void geneLine(long line) throws Exception{
    	File file = makeResourceFile();
	    RandomAccessFile randomFile = null;  
	    try {     
	        // 打开一个随机访问文件流，按读写方式     
	        randomFile = new RandomAccessFile(file, "rw");     
	        StringBuilder strBu=new StringBuilder();
	        // 文件长度，字节数     
	        String str = "";
            int index = 0;
	        while (null != (str = randomFile.readLine())) {
                if (index == line) {//等于写入行号时
                	strBu.append(resource+"\n");
                }
                strBu.append(new String(str.getBytes("ISO-8859-1"),"utf-8")+"\n");
                index++;
            }
	        if(index<line) {
	        	long fileLength = randomFile.length();
	        	randomFile.seek(fileLength); // 将写文件指针移到文件尾。     
		        randomFile.write(("\n"+resource).getBytes());
	        }else {
	        	randomFile.seek(0);
		        randomFile.write(strBu.toString().getBytes());
	        }
	         
	    } catch (IOException e) {     
	        e.printStackTrace();     
	    } finally{  
	        if(randomFile != null){  
	            try {  
	                randomFile.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }
    }
    public static StringBuilder getModelClass(String model) {
	    if(model!=null&&!model.equals("")) {
	    	char[] ch = model.toCharArray();  
		    if (ch[0] >= 'a' && ch[0] <= 'z') {  
		        ch[0] = (char) (ch[0] - 32);  
		    }  
			StringBuilder modelClass = new StringBuilder(new String(ch));
			return modelClass;
	    }else {
	    	return null;
	    }
	}
  //导入实体类拼接
  	public static StringBuilder getImportName(String packagePath,String model) {
  		StringBuilder importName = new StringBuilder();
  		importName.append(packagePath);
  		importName.append(".PO.");
  		importName.append(getModelClass(model));
  		return importName;
  	}
  	public static StringBuilder getImportName(String packagePath,String and,String name) {
  		StringBuilder importName = new StringBuilder();
  		importName.append(packagePath);
  		importName.append(".");
  		importName.append(and);
  		importName.append(".");
  		importName.append(name);
  		return importName;
  	}
}
